package cn.cycleif.intercept;

import cn.cycleif.util.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Optional;

/**
 * &#064;Date: 24/05/22-16:48
 * &#064;Description:
 */

public class PageableItcpt implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    String currentPage = request.getParameter("pageNo");
    String pageSize    = Optional.ofNullable(request.getParameter("pageSize")).orElse("10");
    if (StringUtils.isNoneBlank(currentPage))
      PageUtil.setCurrentPage(new Page<>(Long.parseLong(currentPage), Long.parseLong(pageSize)));
    return true;
  }

  @Override
  public void afterCompletion(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler, Exception ex) {
    PageUtil.remove();
  }

}
